package com.afdor.rws.event.channel.redis;

import com.afdor.rws.annotation.extension.Extension;
import com.afdor.rws.event.Event;
import com.afdor.rws.event.channel.client.producer.AbstractEventProducer;
import com.afdor.rws.event.channel.client.producer.EventProducer;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 基于redis实现的事件生产者实现
 *
 * @author 悭梵
 * @date Created in 2018-08-17 10:23
 */
@Slf4j
@ToString
@EqualsAndHashCode
@Extension(value = "redis", order = Extension.NORMAL_ORDER - 100, scope = Extension.Scope.PROTOTYPE, interfaceClass = EventProducer.class)
public class RedisEventProducer extends AbstractEventProducer {

    @Getter
    @Setter
    private RedisTemplate redisTemplate;

    public RedisEventProducer(@NonNull RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public void doSendEvent(Event event) {
        redisTemplate.opsForList().rightPush(RedisUtils.getQueueName(event.getEventSourceName()), getSerialization().serialize(event));
    }
}
